<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="24" :xs="24">
<!-- 
        <el-header style="text-align: left; font-size: 18px; background:white">
          <el-divider direction="vertical"></el-divider>
            <span> 商品信息</span>
        </el-header>
 -->
        <el-main>
          <el-card class="box-card">
            <div slot="header" class="clearfix" style="line-height: 30px">
              <span><i class="el-icon-search"></i> 筛选查询</span>
              <el-divider direction="vertical"></el-divider>
              <span><i class="el-icon-plus"></i> 新增商品</span>
              <el-row style="float: right">
              <el-col>
                <el-button-group>
                  <el-button type="primary" icon="el-icon-search" 
                    size="small" @click="handleQuery">查询</el-button>
                  <el-button type="primary" icon="el-icon-refresh" 
                    size="small" @click="resetQuery">重置</el-button>
                  <el-button type="primary" icon="el-icon-plus" 
                    size="small" @click="handleAdd"
                  >新增商品</el-button>
                </el-button-group>
              </el-col>
              </el-row>
            </div>

            <div class="text-item">
              <el-form inline="true" :model="queryParams" ref="queryForm">
              <el-form-item label="商品编号：" prop="clId">
                <el-input v-model="queryParams.clId" placeholder="请输入商品编号"
                  clearable @keyup.enter.native="handleQuery"></el-input>
              </el-form-item>
              <el-form-item label="服饰类别：" prop="clCategoryId">
                <el-select v-model="queryParams.clCategoryId" clearable placeholder="请选择">
                  <el-option v-for="item in clCategoryOptions" :key="item.clCategoryId" :label="item.clCategoryName" :value="item.clCategoryId" @keyup.enter.native="handleQuery"></el-option>
                </el-select>
              </el-form-item><!-- 
              <el-form-item label="服饰材质：" prop="clMaterialId">
                <el-select v-model="value" clearable placeholder="请选择">
                  <el-option v-for="item in clMaterialOptions" :key="item.clMaterialId" :label="item.clMaterialName" :value="item.clMaterialId" @keyup.enter.native="handleQuery"></el-option>
                </el-select>
              </el-form-item> -->
              </el-form>
            </div>
          </el-card>
          
          <el-divider></el-divider>

          <el-card class="box-card">
            <div slot="header" class="clearfix" style="line-height: 30px">
              <span><i class="el-icon-s-unfold"></i> 服饰列表</span><el-divider direction="vertical"></el-divider>
            </div>
            <el-row>
            <el-col>
            <div class="text-item">
              <el-table v-loading="loading" :data="clothesList">
                <el-table-column label="商品编号" align="center" prop="clId" />
                <el-table-column label="服饰名称" align="center" prop="clName" />
                <el-table-column label="类别编号" align="center" prop="clCategoryId" />
                <el-table-column label="类别名称" align="center" prop="gsCategory.clCategoryName" />
                <el-table-column label="服饰材质" align="center" prop="clMaterial" />
                <el-table-column label="服饰价格" align="center" prop="clSaleprice" />
                <el-table-column label="服饰成本" align="center" prop="clCost" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="140">
                  <template slot-scope="scope">
                    <el-button
                      size="small"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      >修改</el-button>
                    <el-button
                      size="small"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      >删除</el-button>
                 </template>
                </el-table-column>
              </el-table>

              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[5, 10, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                align="center"
              >
              </el-pagination>
            </div>
            </el-col>
            </el-row>
          </el-card>

        </el-main>
      </el-col>
    </el-row>

    
    <!-- 添加修改服饰信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服饰编号" prop="clId">
          <el-input v-model="form.clId" placeholder="请输入服饰编号" />
        </el-form-item>
        <el-form-item label="服饰名称" prop="clName">
          <el-input v-model="form.clName" placeholder="请输入服饰名称" />
        </el-form-item>
        <el-form-item label="服饰类别" prop="clCategoryId">
          <el-select v-model="form.clCategoryId" clearable placeholder="请选择">
            <el-option v-for="item in clCategoryOptions" :key="item.clCategoryId" :label="item.clCategoryName" :value="item.clCategoryId" @keyup.enter.native="handleQuery"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服饰材质" prop="clMaterial">
          <el-input v-model="form.clMaterial" placeholder="请输入服饰材质" />
        </el-form-item><!-- 
        <el-form-item label="服饰尺码" prop="clSize">
          <el-select v-model="form.clSize" clearable placeholder="请选择">
            <el-option v-for="item in options" 
              :key="item.value" 
              :label="item.label" 
              :value="item.value" 
              @keyup.enter.native="handleEnterclothes">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="服饰价格" prop="clSaleprice">
          <el-input v-model="form.clSaleprice" placeholder="请输入服饰价格" />
        </el-form-item>
        <el-form-item label="服饰成本" prop="clCost">
          <el-input v-model="form.clCost" placeholder="请输入服饰成本" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .clearfix {
    overflow: auto;
  }
  .el-header{
    background-color: #ffffff;
    color: #333;
    width: 100%;
    text-align: left;
    line-height: 50px;
  }

</style>

<script>

import { listClothesByPage,getClothesByclId, addClothes, updateClothes, deleteClothes, getClothesByclCategory } from "@/api/clothes";
import { listClothesCategory } from "@/api/cl_category";
export default {
   name: 'Clothes',
   data(){
    return{
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 当前页
      pageNum: 1,
      // 每页数据量
      pageSize: 5,
/*       // 服饰表格数据
      clothesList: [], */
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //转换
      tranParam:{
        attrsId: undefined,
      },
      //参数存储
      keepParam:{
        pageNum: 1,
        pageSize: 5,
        kpclId: undefined,
      },
      keepCateParam:{
        pageNum: 1,
        pageSize: 5,
        cateId: undefined,
      },
      //服饰尺码
      options: [{
          value: 'S',
          label: 'S'
        }, {
          value: 'M',
          label: 'M'
        }, {
          value: 'L',
          label: 'L'
        }],
      // 服饰类型
      clCategoryOptions: undefined,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        clId: undefined,
        clCategoryId: undefined,
      },


      // 表单参数
      form: {
      },
      rules: {
        clId: [
          { required: true, message: "商品编号不能为空", trigger: "blur" },
        ],
        clCategoryName: [
          { required: true, message: "服饰类别不能为空", trigger: "blur" },
        ],
      },

    }
  },

  created() {
    this.getList();
    // 查询所有clCategory赋值clCategoryOptions
    listClothesCategory().then((response) => {
      this.clCategoryOptions = response.data;
    });

  },

  methods: {

    /** 查询服饰列表 */
    getList() {
      this.loading = true;
      listClothesByPage(this.queryParams).then((response) => {
        this.clothesList = response.data.list;
        this.total = response.data.total;
        this.pageNum = response.data.pageNum;
        this.pageSize = response.data.pageSize;
        this.loading = false;
      })
    },
    getClothesList(){
      this.loading = true;
      if(this.keepParam.kpclId){
        getClothesByclId(this.keepParam.kpclId).then((response) => {
        this.clothesList = response.data;
        if(response.data[0]){
          this.$message.success("查询成功");
        }else{
          this.$message.error("无数据");
        };
        this.loading = false;
        });
        
      }
      
    },
    getClothesByCateList(){
      this.loading = true;
      if(this.keepCateParam.cateId){
        getClothesByclCategory(this.keepCateParam).then((response) => {
        this.clothesList = response.data.list;
        this.total = response.data.total;
        this.pageNum = response.data.pageNum;
        this.pageSize = response.data.pageSize;
        this.loading = false;
        });
      }
    },

    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
      this.getClothesList();
      this.getClothesByCateList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
      this.getClothesList();
      this.getClothesByCateList();
    },

        /** 取消按钮 */
    cancel() {
      this.open = false;
      this.resetForm();
    },

    /** 表单重置 */
    resetForm() {
      this.form = {
        clId: undefined,
        clName: undefined,
        clCategoryId: undefined,
        clMaterial: undefined,
        clSize: undefined,
        clInventory: undefined,
        clSalePrice: undefined,
        clCost: undefined
      };
    },
    resetqueryParam(){
      this.queryParams = {
        clId: undefined,
        clCategory_name: undefined,
      };
    },
    resetkeepParam() {
      this.keepParam = {
        kpclId: undefined,
      };
      this.keepCateParam = {
        cateId: undefined,
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      if(this.queryParams.clId||this.queryParams.clCategoryId){
        this.keepParam.kpclId = this.queryParams.clId;
        this.keepCateParam.cateId = this.queryParams.clCategoryId; 
        this.getClothesList();
        this.getClothesByCateList();
        this.resetqueryParam();
      }else{
        this.$message.error("查询条件不能为空");
      }
      
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 5,
        clId: undefined,
        clCategory_name: undefined,
      };
      this.keepParam = {
        pageNum: 1,
        pageSize: 5,
        kpclId: undefined,
      }
      this.keepCateParam = {
        pageNum: 1,
        pageSize: 5,
        kpclCategory: undefined,
      }
      this.getList();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm();
      this.open = true;
      this.title = "添加服饰";
      
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      /* this.resetForm(); *//* 
      this.tranParam.attrsId = row.clId; */
      getClothesByclId(row.clId).then((response) => {
        const List = response.data;
        this.form = List[0];
        this.open = true;
        this.title = "修改服饰";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title == "修改服饰") {
            updateClothes(this.form).then((response) => {
              this.open = false;
              this.title = "";
              this.getList();
              this.$message.success("修改完成"); //后端返回消息字符串
            });
          } else {
            const flag = true;
/*             this.$message.success(this.form.clName+this.form.clMaterial);
 */            addClothes(this.form).then((response) => {
              if(!response.data){
                flag = response.data;
              }
            });
            if(flag){
                this.$message.success("添加成功");
              }else{
                this.$message.error("添加失败");
              }
            this.open = false;
            this.title = "";
            this.getList();
          }
        }
      });
      
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除' + row.clName + '?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
       }
      )
      .then(function () {
        return deleteClothes(row.clId);
      })
      .then(() => {
        this.getList();
        this.$message.success("删除成功");
      })
      .catch(() => {});
    }
  }

    
   
}
</script>